/**
 * e3db Fast database library for Grails
 * Copyright (C) 2009-2010 Collegeman.net, LLC
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package net.collegeman.grails.e3db;

import java.util.*;
import groovy.lang.*;
import org.codehaus.groovy.runtime.*;
import org.springframework.jdbc.support.rowset.*;

public class GroovySqlRowSet extends GroovyObjectSupport {
	
	private SqlRowSet rs;	

	// never persist the MetaClass
    private transient MetaClass metaClass;

	private transient MetaClass rsMetaClass;

	public GroovySqlRowSet(SqlRowSet sqlRowSet) {
		if (sqlRowSet == null)
			throw new IllegalArgumentException("SqlRowSet parameter cannot be null.");

		this.rs = sqlRowSet;
		this.metaClass = InvokerHelper.getMetaClass(this.getClass());
		this.rsMetaClass = InvokerHelper.getMetaClass(rs.getClass());
	}

	public Object getProperty(String property) {
		try {
			return rs.getObject(property);
		} catch (org.springframework.jdbc.InvalidResultSetAccessException e) {
			return getMetaClass().getProperty(rs, property);
		}
	}

	public Object invokeMethod(String name, Object args) {
		if ("next".equals(name)) {
			return rs.next();
		} 
		else {
			return getMetaClass().invokeMethod(this.rs, name, args);
		}
	}

	public void setProperty(String property, Object newValue) {
        getMetaClass().setProperty(this.rs, property, newValue);
    }

    public MetaClass getMetaClass() {
        if (metaClass == null) {
            metaClass = InvokerHelper.getMetaClass(getClass());
        }
        return metaClass;
    }

    public void setMetaClass(MetaClass metaClass) {
        this.metaClass = metaClass;
    }

    public Object get(int index) {
    	// move cursor to position index
    	rs.absolute(index+1);
    	// return reference to self, thereby creating the illusion of a List containing individual elements
    	return this;
    }

    public Iterator iterator() {
    	final GroovySqlRowSet parent = this;

    	return new Iterator() {
    		public boolean hasNext() {
    			return rs.next();
    		}

    		public Object next() {
    			return parent;
    		}

    		public void remove() {
    			throw new IllegalStateException("Cannot remove items from a SqlRowSet.");
    		}
    	};
    }

    public int size() {
    	if (rs.next()) {
    		rs.absolute(1);
    		return 1;
    	}
    	else {
    		return 0;
    	}
    }


	
}